package com.mg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.DataCodeDto;
import com.mg.dto.SimpleResult;
import com.mg.mapper.DataCodeMapper;
import com.mg.model.DataCode;
import com.mg.service.DataCodeService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by mg1213 on 2018/3/18.
 */
@Service
public class DataCodeServiceImpl implements DataCodeService {
    @Autowired
    private DataCodeMapper dataCodeMapper;

    private Logger logger = LogManager.getLogger(DataCodeServiceImpl.class.getName());

    @Override
    public PageInfo<DataCodeDto> selectPageInfo(DataCodeDto dataCodeDto, int start, int limit) {
        PageHelper.offsetPage(start, limit);
        List<DataCodeDto> resultList = dataCodeMapper.selectPageInfo(dataCodeDto);
        PageInfo<DataCodeDto> result = new PageInfo<>(resultList);
        return result;
    }

    @Override
    public List<DataCode> select(DataCode dataCode) {
        return dataCodeMapper.select(dataCode);
    }

    @Override
    public SimpleResult insertDataCode(DataCode record) {
        SimpleResult result = new SimpleResult();
        try {
        
            dataCodeMapper.insert(record);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("新增字典信息失败 系统未知异常");
            logger.error("新增字典信息失败 系统未知异常", e);
        }
        return result;
    }

    @Override
    public SimpleResult updateDataCode(DataCode record) {
        SimpleResult result = new SimpleResult();
        try {
            dataCodeMapper.update(record);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("更新字典信息失败 系统未知异常");
            logger.error("更新字典信息失败 系统未知异常", e);
        }
        return result;
    }

    @Override
    public SimpleResult deleteDataCode(String ids) {
        SimpleResult result = new SimpleResult();
        try {
            String[] idArr = ids.split(",");
            for (String id : idArr) {

                dataCodeMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("删除字典信息失败 系统未知异常");
            logger.error("删除字典信息失败 系统未知异常", e);
        }
        return result;
    }
}
